package cloud.simple;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WatchClient implements Runnable {

	private final Logger LOG = LoggerFactory.getLogger(WatchClient.class);
	public static String PATH = null;
	private static ZooKeeper zk;
	private static Stat stat = new Stat();
	//结点列表
	private static List<String> nodeList;
	//zookeeper 数据Map
	public static HashMap<String, String> zkMap = new HashMap<String, String>();

	public WatchClient(String uri,String znode,int times) throws IOException {

		PATH = znode;
		zk = new ZooKeeper(uri , 21810, new Watcher() {
			public void process(WatchedEvent event) {
				
			}
		});
	}
	
	/**
	 * 设置watch的线程
	 */
	public void run() {

		System.out.println("进入线程run方法");
		Watcher wc = new Watcher() {
			public void process(WatchedEvent event) {
				List<String> nodeListBefore = nodeList;
				
				LOG.info("节点数据改变:" + event.getPath());
				try {
					if (event.getType() == EventType.NodeDataChanged) {
						LOG.info("节点信息改变:" + event.getPath());
					}
					if (event.getType() == EventType.NodeDeleted) {
						LOG.info("节点被删:" + event.getPath());
					}
					if (event.getType() == EventType.NodeCreated) {
						LOG.info("新增节点:" + event.getPath());
					}
					updateServerList();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				try {
					nodeList = zk.getChildren(event.getPath(), false);
				} catch (KeeperException e) {
					System.out.println(event.getPath() + "没有子节点已经被删除");
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				List<String> nodeListNow = nodeList;
				if (nodeListBefore.size() < nodeListNow.size()) {
					for (String str : nodeListNow) {
						if (!nodeListBefore.contains(str)) {
							LOG.info("创建节点:" + event.getPath() + "/" + str);
						}
					}
				}
			}
		};

		while (true) {
			try {
				zk.exists(PATH, wc);
			} catch (KeeperException | InterruptedException e) {
				e.printStackTrace();
			}
			try {
				nodeList = zk.getChildren(PATH, wc);
			} catch (KeeperException | InterruptedException e) {
				e.printStackTrace();
			}
			
			for (String nodeName : nodeList) {
				try {
					zk.exists(PATH + "/" + nodeName, wc);
					nodeList = zk.getChildren(PATH + "/" + nodeName, wc);
				} catch (KeeperException | InterruptedException e) {
					e.printStackTrace();
				}
			}

			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void updateServerList() throws Exception {
		zkMap.clear();
		List<String> subList = zk.getChildren(PATH, true);
		for (String subNode : subList) {
			byte[] data = zk.getData(PATH + "/" + subNode, false, stat);
			zkMap.put(subNode, new String(data, "utf-8"));
		}

		for (Map.Entry<String, String> entry : zkMap.entrySet()) {
			String e = entry.getKey()+"=====>"+entry.getValue();
			System.out.println(e);
		}
	}
}
